<?php

namespace User\Service;
use Zend\Authentication\AuthenticationServiceInterface as AuthenticationService;
use Zend\Form\FormInterface as Form;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use User\Mapper\HydratorInterface as Hydrator;
//use User\Mapper\UserInterface as UserMapper;
use User\Mapper\UserInterface as UserMapper;

use User\Options\UserServiceOptionsInterface as ServiceOptions;

use Base\EventManager\EventProvider;

class User extends EventProvider implements ServiceManagerAwareInterface
{
    protected $serviceManager;
    /**
     * createFromForm
     *
     * @param array $data
     * @return \User\Entity\UserInterface
     * @throws Exception\InvalidArgumentException
     */
    
    public function register(array $data)
    {
        $entityClass = $this->getOptions()->getUserEntityClass();
        $userEntity = new $entityClass;

        $form = $this->getRegisterForm();
        $formHydrator = $this->getFormHydrator();
        $form->setHydrator($formHydrator);
        $form->bind($userEntity);
        $form->setData($data);
        if($form->isValid())
        {
            $user = $form->getData();
            $events= $this->getEventManager();

            $user->setPassword($this->getFormHydrator()->getCryptoService()->create($user->getPassword()));

            //$events->trigger(__FUNCTION__, $this, compact('user', 'form'));
            $userMapper = $this->getUserMapper()->insert($user);
                //$events->trigger(__FUNCTION__.'.post', $this, compact('user', 'form'));
            var_dump($userMapper);
                return $user;
        }
        return NULL;
    }
    
    protected $options;
    public function getOptions()
    {	
	if(!$this->options instanceof ServiceOptions)
	{
	    $this->setOptions($this->serviceManager->get('user_module_options'));
	}
	return $this->options;
    }
    
    public function setOptions(ServiceOptions $options)
    {
	$this->options = $options;
    }
    
    protected $registerForm;
    public function getRegisterForm()
    {
	if(!$this->registerForm)
	{
	    $this->setRegisterForm($this->serviceManager->get('user_register_form'));
	}
	return $this->registerForm;
    }
    
    public function setRegisterForm(Form $registerForm)
    {
	$this->registerForm = $registerForm;
    }
    
    protected $formHydrator;
    public function getFormHydrator()
    {	 	
	if(!$this->formHydrator instanceof Hydrator)
	{	   
	    $this->setFormHydrator($this->serviceManager->get('user_user_hydrator'));
	}
	return $this->formHydrator;
    }
    
    public function setFormHydrator($formHydrator)
    {
	$this->formHydrator = $formHydrator;
    }
    
    protected $userMapper;
    public function getUserMapper()
    {
	if(null === $this->userMapper)
	{
	    $this->setUserMapper($this->serviceManager->get('user_user_mapper'));
	}
	return $this->userMapper;
    }
    
    public function setUserMapper(UserMapper $userMapper)
    {
	$this->userMapper = $userMapper;
	return $this;
    }
    /**
     * Set service manager instance
     *
     * @param ServiceManager $serviceManager
     * @return User
     */
    public function setServiceManager(ServiceManager $serviceManager)
    {
        $this->serviceManager = $serviceManager;
        return $this;
    }
}
